package com.dddpeter.yater;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.*;
import android.view.View.OnTouchListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import java.util.ArrayList;


public class MainActivity extends Activity {


    final int MSG_SEND = 0x100;
    final int MSG_CANCLE = 0x101;
    final int MSG_CLEAR = 0x102;
    final int MSG_DRAW_CIRCLE = 0x103;
    final int MSG_SHOW_SETUP = 0x104;
    final int MSG_REGISTER = 0x105;
    private float x;
    private float y;
    DrawView draw;
    FlowerView flowerView;
    RadioGroup rg;
    RadioButton rb1;
    RadioButton rb2;
    RadioButton rb3;
    int flowerType = R.drawable.meigui;
    final Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            NotificationManager notiManger = (NotificationManager) MainActivity.this.getSystemService(MainActivity.this.NOTIFICATION_SERVICE);
            PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0,
                    new Intent(MainActivity.this, Tab2Activity.class), 0);
            Notification notify = new Notification();
            switch (msg.what) {
                case MSG_SEND:
                    notify.icon = R.drawable.ic_launcher;
                    notify.tickerText = "�Ѿ��л���tab1";
                    notify.when = System.currentTimeMillis();
                    notify.defaults = Notification.DEFAULT_SOUND;
                    notify.setLatestEventInfo(MainActivity.this, "Yater��Ϣ", "Click to view", contentIntent);
                    notiManger.notify(MSG_SEND, notify);
                    break;
                case MSG_CANCLE:
                    notiManger.cancel(MSG_SEND);
                    break;
                case MSG_CLEAR:
                    DrawView.circleArray.clear();
                    DrawView.draw = true;
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                        DrawView.draw = false;
                    }
                    break;
                case MSG_DRAW_CIRCLE:
                    Circle c = new Circle();
                    c.setmX(MainActivity.this.x);
                    c.setmY(MainActivity.this.y);
                    c.setRadius((float) (Math.random() * 70 + 30));
                    int RED = (int) (Math.random() * 255 + 1);
                    int GREEN = (int) (Math.random() * 255 + 1);
                    int BLUE = (int) (Math.random() * 255 + 1);
                    Color color = new Color();
                    c.setColor(color.rgb(RED, GREEN, BLUE));
                    draw.circleArray.add(c);
                    draw.invalidate();
                    break;
                case MSG_SHOW_SETUP:
                    Intent intent = new Intent(MainActivity.this, YaterSetupActivity.class);
                    startActivity(intent);
                    break;
                case MSG_REGISTER:
                    intent = new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(intent);
                    break;
            }
            super.handleMessage(msg);
        }

    };


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.exit:
                this.finish();
                System.exit(0);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void sendNotify(View view) {

        Message msg = new Message();
        msg.what = MSG_SEND;
        handler.sendMessage(msg);
    }

    public void cancleNotify(View view) {

        Message msg = new Message();
        msg.what = MSG_CANCLE;
        handler.sendMessage(msg);
    }

    public void clearAll(View view) {
        Message msg = new Message();
        msg.what = MSG_CLEAR;
        handler.sendMessage(msg);
    }

    public void showSetup(View view) {
        Message msg = new Message();
        msg.what = MSG_SHOW_SETUP;
        handler.sendMessage(msg);
    }
    public void  register(View view) {
        Message msg = new Message();
        msg.what = MSG_REGISTER;
        handler.sendMessage(msg);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final PagerTabStrip pageTitle = (PagerTabStrip) findViewById(R.id.pagertitle);
        ViewPager viewPager = (ViewPager) findViewById(R.id.guidePages);
        pageTitle.setTabIndicatorColor(0x993333);
        //pageTitle.setDrawFullUnderline(true);
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view1 = layoutInflater.inflate(R.layout.tab1, null);
        View view2 = layoutInflater.inflate(R.layout.tab2, null);
        View view3 = layoutInflater.inflate(R.layout.activity_flower_grow, null);
        draw = (DrawView) view2.findViewById(R.id.drawView1);
        flowerView = (FlowerView) view3.findViewById(R.id.flowerView1);
        rg = (RadioGroup) view3.findViewById(R.id.radioGroup1);
        rb1 = (RadioButton) view3.findViewById(R.id.radio0);
        rb2 = (RadioButton) view3.findViewById(R.id.radio1);
        rb3 = (RadioButton) view3.findViewById(R.id.radio2);

        final ArrayList<View> views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);
        views.add(view3);
        final ArrayList<String> titles = new ArrayList<String>();
        titles.add("Notification");
        titles.add("DrawTest");
        titles.add("Grow flowers");

        @SuppressWarnings("unused")
        PagerAdapter mPagerAdapter = new PagerAdapter() {

            @Override
            public int getCount() {

                return views.size();
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }

            @Override
            public void destroyItem(View container, int position, Object object) {
                ((ViewPager) container).removeView(views.get(position));
                //pageTitle.setTextColor(0x993333);
            }

            @Override
            public CharSequence getPageTitle(int position) {

                return titles.get(position);
                //#990033

            }

            @Override
            public Object instantiateItem(View container, int position) {
                ((ViewPager) container).addView(views.get(position));

                return views.get(position);
            }

        };
        viewPager.setAdapter(mPagerAdapter);
        OnTouchListener drawTouchListener = new OnTouchListener() {


            public boolean onTouch(View arg0, MotionEvent event) {
                MainActivity.this.x = event.getX();
                MainActivity.this.y = event.getY();
                Message msg = new Message();
                msg.what = MSG_DRAW_CIRCLE;
                handler.sendMessage(msg);
                return true;
            }

        };
        draw.setOnTouchListener(drawTouchListener);
        flowerView.setFlowerType(flowerType);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup arg0, int id) {
                if (id == rb1.getId()) {
                    flowerType = R.drawable.meigui;
                } else if (id == rb2.getId()) {
                    flowerType = R.drawable.hehua;
                } else if (id == rb3.getId()) {
                    flowerType = R.drawable.juhua;
                }

                flowerView.setFlowerType(flowerType);

            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);


        return true;
    }


}
